;4x4 pixel ball bounces around the screen

.DATA
	SCREEN = $F000
	DIRX = $0000
	DIRY = $0000
	COLOR = $1111
	LOCATION = $FC81
.CODE
	LOAD R0 #0
	LOAD R15 SCREEN
	LOAD R1 #124
	LOAD R2 COLOR
	LOAD R3 DIRX
	LOAD R4 DIRY
	LOAD R5 LOCATION
	;TMP VARS
	LOAD R13 $20

BOUNCE:
	;DIR 0 IS --
	;DIR 1 IS ++
	JMP DRAWBALL
	FROMDRAWBALL:
	;JMP ERASEBALL
	;FROMERASEBALL:
	JMP MOVEBALL	

DRAWBALL:
	TRAN R5 R14
	STORE R2 R14
	CLC
	ADD R14 R13
	STORE R2 R14
	ADD R14 R13
	STORE R2 R14
	ADD R14 R13
	STORE R2 R14
	JMP FROMDRAWBALL

ERASEBALL:	
	TRAN R5 R14
	STORE R0 R14
	CLC
	ADD R14 R13
	STORE R0 R14
	ADD R14 R13
	STORE R0 R14
	ADD R14 R13
	STORE R0 R14
	;JMP FROMERASEBALL
	
MOVEBALL:
	;CHECK Y
	TRAN R5 R14
	LOAD R12 $0FFF
	AND R14 R12
	LOAD R12 $0080
	DIV R14 R12
	TRAN R12 R11	;KEEP X LOCATION IN R11
		JMP YMOVE
		AFTERYMOVE:
	CMP R14 R0
	JEQ SWITCHDIRY
	LOAD R12 $001F
	CMP R14 R12
	JEQ SWITCHDIRY
	
	RESETLOCY:
	LOAD R12 $0080
	MULT R14 R12
	LOAD R12 $F000
	OR R14 R12
	TRAN R14 R5
	
	JMP XMOVE
	AFTERXMOVE:
	CMP R11 R0
	JEQ SWITCHDIRX
	LOAD R12 $001F
	CMP R11 R12
	JEQ SWITCHDIRX
	
	RESETLOCX:
	CLC
	ADD R5 R11
	
	ADJUSTCOLOR:
	LOAD R12 $1111
	CLC
	ADD R2 R12
	
	JMP BOUNCE


YMOVE:
	CMP R4 R0
	JEQ YUP; IF DIRY = 0
		INC R14
		JMP AFTERYMOVE
	YUP:
		DEC R14
		JMP AFTERYMOVE
		
XMOVE:
	CMP R3 R0
	JEQ XLEFT; IF DIRX = 0
		INC R11
		JMP AFTERXMOVE
	XLEFT:
		DEC R11
		JMP AFTERXMOVE		
		
SWITCHDIRY:
	LOAD R12 $0001
	XOR R4 R12
	JMP RESETLOCY
	
SWITCHDIRX:
	LOAD R12 $0001
	XOR R3 R12
	JMP RESETLOCX